Completed
Pull Request — master (#99)
by Johan
01:15
created

UPC.js ➔ ???   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 22
rs 9.2
c 4
b 0
f 0
1
// Encoding documentation:
2
// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding
3
4
import EANencoder from './ean_encoder.js';
5
6
class UPC{
7
	constructor(string, options){
8
		// Add checksum if it does not exist
9
		if(string.search(/^[0-9]{11}$/) !== -1){
10
			this.string = string + this.checksum(string);
11
		}
12
		else{
13
			this.string = string;
14
		}
15
16
		this.displayValue = options.displayValue;
17
18
		// Make sure the font is not bigger than the space between the guard bars
19
		if(options.fontSize > options.width * 10){
20
			this.fontSize = options.width * 10;
21
		}
22
		else{
23
			this.fontSize = options.fontSize;
24
		}
25
26
		// Make the guard bars go down half the way of the text
27
		this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
28
	}
29
30
	valid(){
31
		return this.string.search(/^[0-9]{12}$/) !== -1 &&
32
			this.string[11] == this.checksum(this.string);
33
	}
34
35
	encode(){
36
		var encoder = new EANencoder();
37
		var result = [];
38
39
		// Add the first digigt
40
		if(this.displayValue){
41
			result.push({
42
				data: "00000000",
43
				text: this.string[0],
44
				options: {textAlign: "left", fontSize: this.fontSize}
45
			});
46
		}
47
48
		// Add the guard bars
49
		result.push({
50
			data: "101" + encoder.encode(this.string[0], "L"),
51
			options: {height: this.guardHeight}
52
		});
53
54
		// Add the left side
55
		result.push({
56
			data: encoder.encode(this.string.substr(1, 5), "LLLLL"),
57
			text: this.string.substr(1, 5),
58
			options: {fontSize: this.fontSize}
59
		});
60
61
		// Add the middle bits
62
		result.push({
63
			data: "01010",
64
			options: {height: this.guardHeight}
65
		});
66
67
		// Add the right side
68
		result.push({
69
			data: encoder.encode(this.string.substr(6, 5), "RRRRR"),
70
			text: this.string.substr(6, 5),
71
			options: {fontSize: this.fontSize}
72
		});
73
74
		// Add the end bits
75
		result.push({
76
			data: encoder.encode(this.string[11], "R") + "101",
77
			options: {height: this.guardHeight}
78
		});
79
80
		// Add the last digit
81
		if(this.displayValue){
82
			result.push({
83
				data: "00000000",
84
				text: this.string[11],
85
				options: {textAlign: "right", fontSize: this.fontSize}
86
			});
87
		}
88
89
		return result;
90
	}
91
92
	// Calulate the checksum digit
93
	// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
94
	checksum(number){
95
		var result = 0;
96
97
		var i;
98
		for(i = 1; i < 11; i += 2){
99
			result += parseInt(number[i]);
100
		}
101
		for(i = 0; i < 11; i += 2){
102
			result += parseInt(number[i]) * 3;
103
		}
104
105
		return (10 - (result % 10)) % 10;
106
	}
107
}
108
109
export default UPC;
110